package com.mybatisplus.demo.android.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mybatisplus.demo.android.dao.DeviceMapper;
import com.mybatisplus.demo.android.dao.UserMapper;
import com.mybatisplus.demo.android.enmu.ApiResponse;
import com.mybatisplus.demo.android.enmu.ResultCode;
import com.mybatisplus.demo.android.model.DeviceInfo;
import com.mybatisplus.demo.android.model.User;
import com.mybatisplus.demo.android.rep.DeviceRep;
import com.mybatisplus.demo.android.service.DeviceService;
import com.mybatisplus.demo.android.util.CollectionUtil;
import com.mybatisplus.demo.android.viewobject.DeviceVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeviceServiceimpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;//与dao层进行交互

    //将核心模型转化为UI可用模型
    private DeviceVo convertFromModel(DeviceInfo deviceInfo) {
        if (null == deviceInfo) {
            return null;
        }
        DeviceVo deviceVo = new DeviceVo();
        BeanUtils.copyProperties(deviceInfo, deviceVo);
        return deviceVo;
    }

    /**
     * 新增设备
     *
     * @param rep
     * @return
     */
    @Override
    public ApiResponse addDevice(DeviceRep rep) {
        ApiResponse apiResponse = new ApiResponse();


        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeveice_sn(rep.getDeveice_sn());
        QueryWrapper<DeviceInfo> queryWrapper = new QueryWrapper<>(deviceInfo);
        DeviceInfo selectInfo = deviceMapper.selectOne(queryWrapper);
        if (selectInfo == null) {
            deviceInfo.setCreate_date("" + LocalDateTime.now());
            deviceInfo.setUpdate_date("" + LocalDateTime.now());
            deviceInfo.setDeveice_model(rep.getDeveice_model().trim());
            deviceInfo.setDeveice_name(rep.getDeveice_name().trim());
            deviceInfo.setDeveice_state(rep.getDeveice_state().trim());
            deviceInfo.setDeveice_type(rep.getDeveice_type().trim());
            deviceInfo.setManufacturer(rep.getManufacturer().trim());
            deviceInfo.setProduction_date(rep.getProduction_date().trim());
            deviceInfo.setUserId(rep.getUserId());
            //插入新设备 返回变更行数
            int i = deviceMapper.insert(deviceInfo);
            if (i > 0) {
                //新建成功
                apiResponse.setResultCode(ResultCode.SUCCESS);
                apiResponse.setMessage("添加成功");
            } else {
                apiResponse.setResultCode(ResultCode.ADD_DEVICE_ERROR);
            }
        } else {
            apiResponse.setResultCode(ResultCode.DEVICE_SN_ISEXIST);
        }

        return apiResponse;
    }

    /**
     * 删除设备
     *
     * @return
     */
    @Override
    public ApiResponse deleteDevice(String deveice_sn) {
        ApiResponse apiResponse = new ApiResponse();

        if (StringUtils.isEmpty(deveice_sn)) {
            apiResponse.setResultCode(ResultCode.PARAM_ERROR);
            return apiResponse;
        }

        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeveice_sn(deveice_sn);
        QueryWrapper<DeviceInfo> queryWrapper = new QueryWrapper<>(deviceInfo);
        DeviceInfo selectInfo = deviceMapper.selectOne(queryWrapper);
        if (selectInfo == null) {
            apiResponse.setResultCode(ResultCode.DEVICE_NOT_ISEXIST);
        } else {

            int i = deviceMapper.delete(queryWrapper);
            if (i > 0) {
                //删除成功
                apiResponse.setResultCode(ResultCode.SUCCESS);
                apiResponse.setMessage("删除成功");
            } else {
                apiResponse.setResultCode(ResultCode.DETELE_DEVICE_ERROR);
            }
        }
        return apiResponse;
    }

    /**
     * 更新设备信息
     *
     * @param deviceRep 设备信息
     * @return
     */
    @Override
    public ApiResponse updateDevice(DeviceRep deviceRep) {
        ApiResponse apiResponse = new ApiResponse();

        if (StringUtils.isEmpty(deviceRep.getDeveice_sn())) {
            apiResponse.setResultCode(ResultCode.PARAM_ERROR);
            return apiResponse;
        }

        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeveice_sn(deviceRep.getDeveice_sn().trim());
        QueryWrapper<DeviceInfo> queryWrapper = new QueryWrapper<>(deviceInfo);
        deviceInfo = deviceMapper.selectOne(queryWrapper);
        if (deviceInfo == null) {
            apiResponse.setResultCode(ResultCode.DEVICE_NOT_ISEXIST);
        } else {
            //不写queryWrapper条件 会变更全部
            UpdateWrapper userUpdateWrapper = new UpdateWrapper();
            userUpdateWrapper.eq("deveice_sn", deviceInfo.getDeveice_sn().trim()); //不写queryWrapper条件 会变更全部

            if (!StringUtils.isEmpty(deviceRep.getDeveice_model())) {
                deviceInfo.setDeveice_model(deviceRep.getDeveice_model().trim());
            }
            if (!StringUtils.isEmpty(deviceRep.getDeveice_name())) {
                deviceInfo.setDeveice_name(deviceRep.getDeveice_name().trim());
            }
            if (!StringUtils.isEmpty(deviceRep.getDeveice_state())) {
                deviceInfo.setDeveice_state(deviceRep.getDeveice_state().trim());
            }
            if (!StringUtils.isEmpty(deviceRep.getDeveice_type())) {
                deviceInfo.setDeveice_type(deviceRep.getDeveice_type().trim());
            }
            if (!StringUtils.isEmpty(deviceRep.getManufacturer())) {
                deviceInfo.setManufacturer(deviceRep.getManufacturer().trim());
            }
            if (!StringUtils.isEmpty(deviceRep.getProduction_date())) {
                deviceInfo.setProduction_date(deviceRep.getProduction_date().trim());
            }

            deviceInfo.setUpdate_date("" + LocalDateTime.now());

            int i = deviceMapper.update(deviceInfo, userUpdateWrapper);
            if (i > 0) {
                //修改成功
                apiResponse.setResultCode(ResultCode.SUCCESS);
                apiResponse.setMessage("修改成功");
                apiResponse.setData(convertFromModel(deviceInfo));
            } else {
                apiResponse.setResultCode(ResultCode.UPDATE_DEVICE_ERROR);
            }
        }

        return apiResponse;
    }

    /**
     * 查询单个设备详情
     *
     * @return
     */
    @Override
    public ApiResponse selectOneDevice(String deveice_sn) {
        ApiResponse apiResponse = new ApiResponse();

        if (StringUtils.isEmpty(deveice_sn)) {
            apiResponse.setResultCode(ResultCode.PARAM_ERROR);
            return apiResponse;
        }

        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeveice_sn(deveice_sn);
        QueryWrapper<DeviceInfo> queryWrapper = new QueryWrapper<>(deviceInfo);
        DeviceInfo selectInfo = deviceMapper.selectOne(queryWrapper);
        if (selectInfo == null) {
            apiResponse.setResultCode(ResultCode.DEVICE_NOT_ISEXIST);
        } else {
            apiResponse.setResultCode(ResultCode.SUCCESS);
            apiResponse.setData(convertFromModel(selectInfo));

        }
        return apiResponse;
    }

    /**
     * 查询设备列表
     */
    @Override
    public ApiResponse selectTypeListDevices(String deveice_type) {
        ApiResponse apiResponse = new ApiResponse();

        QueryWrapper<DeviceInfo> queryWrapper = Wrappers.<DeviceInfo>query();
        Map<String, Object> map = new HashMap<>();
        map.put("deveice_type", deveice_type);
        queryWrapper.allEq(map);
        List<DeviceInfo> list = deviceMapper.selectList(queryWrapper);
        List<DeviceVo> deviceVoList = new ArrayList<>();
        if (list.size() > 0) {
            for (DeviceInfo deviceInfo : list) {
                deviceVoList.add(convertFromModel(deviceInfo));
            }
            apiResponse.setResultCode(ResultCode.SUCCESS);
            apiResponse.setData(deviceVoList);
        } else {
            apiResponse.setResultCode(ResultCode.SUCCESS);
        }

        return apiResponse;
    }

    /**
     * 查询设备列表
     */
    @Override
    public ApiResponse selectUserIdListDevices(String user_id) {
        ApiResponse apiResponse = new ApiResponse();

        //查询列表
        QueryWrapper<DeviceInfo> queryWrapperList = Wrappers.<DeviceInfo>query();
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", user_id);
        queryWrapperList.allEq(map);
        List<DeviceInfo> list = deviceMapper.selectList(queryWrapperList);
        List<DeviceVo> deviceVoList = new ArrayList<>();
        if (list.size() > 0) {
            CollectionUtil.ListSort(list);
            for (DeviceInfo deviceInfo : list) {
                deviceVoList.add(convertFromModel(deviceInfo));
            }
            apiResponse.setResultCode(ResultCode.SUCCESS);
            apiResponse.setData(deviceVoList);
        } else {
            apiResponse.setResultCode(ResultCode.SUCCESS);
        }

        return apiResponse;
    }
}
